﻿namespace PerpetuumSoft.InstrumentationPDA
{
    using PerpetuumSoft.InstrumentationPDA.Mathematics;
    using System;
    using System.Drawing;

    public class Toggle : SwitchBase
    {
        private Color activeLedColor = Color.LightGreen;
        private Color faceColor = Color.DarkGray;
        private PerpetuumSoft.InstrumentationPDA.FaceStyle faceStyle = PerpetuumSoft.InstrumentationPDA.FaceStyle.RaisedNotch;
        private Color inactiveLedColor = Color.Green;
        private Size ledSize = new Size(20, 10);
        private Color notchDarkColor = Color.Gray;
        private Color notchLightColor = SystemColors.ControlLightLight;

        public Toggle()
        {
            this.BackColor = Color.DimGray;
        }

        protected override void DrawDynamic(Graphics g)
        {
            Vector vector3;
            Vector vector5;
            Brush brush;
            base.DrawDynamic(g);
            bool flag = (base.Checked && !base.Reverse) || (!base.Checked && base.Reverse);
            Rectangle workRectangle = this.WorkRectangle;
            Vector vector = new Vector(base.Direction, workRectangle.X, workRectangle.Y);
            Vector vector2 = new Vector(base.Direction, workRectangle.Width, workRectangle.Height);
            Pen pen = new Pen(this.NotchLightColor);
            Pen pen2 = new Pen(this.NotchDarkColor);
            Vector vector4 = new Vector(base.Direction, vector2.Width, vector2.Height / 2M);
            if (flag)
            {
                vector3 = new Vector(base.Direction, vector.X, vector.Y);
            }
            else
            {
                vector3 = new Vector(base.Direction, vector.X, vector.Y + (vector2.Height / 2M));
            }
            g.FillRectangle(new SolidBrush(this.FaceColor), base.Round(vector3.X), base.Round(vector3.Y), base.Round(vector4.Width), base.Round(vector4.Height));
            g.DrawLine(pen, (int) vector3.X, (int) vector3.Y, (int) (vector3.X + vector4.Width), (int) vector3.Y);
            g.DrawLine(pen, (int) vector3.X, (int) vector3.Y, (int) vector3.X, (int) (vector3.Y + vector4.Height));
            /*g.DrawLine(pen, (int) decimal.op_Increment(vector3.X), (int) decimal.op_Increment(vector3.Y), (int) decimal.op_Decrement(vector3.X + vector4.Width), (int) decimal.op_Increment(vector3.Y));
            g.DrawLine(pen, (int) decimal.op_Increment(vector3.X), (int) decimal.op_Increment(vector3.Y), (int) decimal.op_Increment(vector3.X), (int) decimal.op_Decrement(vector3.Y + vector4.Height));
            g.DrawLine(pen2, (int) (vector3.X + vector4.Width), (int) (vector3.Y + vector4.Height), (int) (vector3.X + vector4.Width), (int) vector3.Y);
            g.DrawLine(pen2, (int) (vector3.X + vector4.Width), (int) (vector3.Y + vector4.Height), (int) vector3.X, (int) (vector3.Y + vector4.Height));
            g.DrawLine(pen2, (int) decimal.op_Decrement(vector3.X + vector4.Width), (int) decimal.op_Decrement(vector3.Y + vector4.Height), (int) decimal.op_Decrement(vector3.X + vector4.Width), (int) decimal.op_Increment(vector3.Y));
            g.DrawLine(pen2, (int) decimal.op_Decrement(vector3.X + vector4.Width), (int) decimal.op_Decrement(vector3.Y + vector4.Height), (int) decimal.op_Increment(vector3.X), (int) decimal.op_Decrement(vector3.Y + vector4.Height));*/
            
            //g.DrawLine(pen, (int)(++vector3.X), (int)(++vector3.Y), (int)decimal.op_Decrement(vector3.X + vector4.Width), (int)(++vector3.Y));
            //g.DrawLine(pen, (int)(++vector3.X), (int)(++vector3.Y), (int)(++vector3.X), (int)decimal.op_Decrement(vector3.Y + vector4.Height));
            //g.DrawLine(pen2, (int)(vector3.X + vector4.Width), (int)(vector3.Y + vector4.Height), (int)(vector3.X + vector4.Width), (int)vector3.Y);
            //g.DrawLine(pen2, (int)(vector3.X + vector4.Width), (int)(vector3.Y + vector4.Height), (int)vector3.X, (int)(vector3.Y + vector4.Height));
            //g.DrawLine(pen2, (int)decimal.op_Decrement(vector3.X + vector4.Width), (int)decimal.op_Decrement(vector3.Y + vector4.Height), (int)decimal.op_Decrement(vector3.X + vector4.Width), (int)(++vector3.Y));
            //g.DrawLine(pen2, (int)decimal.op_Decrement(vector3.X + vector4.Width), (int)decimal.op_Decrement(vector3.Y + vector4.Height), (int)(++vector3.X), (int)decimal.op_Decrement(vector3.Y + vector4.Height));

            g.DrawLine(pen, (int)(vector3.X++), (int)(vector3.Y++), (int)(vector3.X + vector4.Width - 1), (int)(vector3.Y++));
            g.DrawLine(pen, (int)(vector3.X++), (int)(vector3.Y++), (int)(vector3.X++), (int)(vector3.Y + vector4.Height - 1));
            g.DrawLine(pen2, (int)(vector3.X + vector4.Width), (int)(vector3.Y + vector4.Height), (int)(vector3.X + vector4.Width), (int)vector3.Y);
            g.DrawLine(pen2, (int)(vector3.X + vector4.Width), (int)(vector3.Y + vector4.Height), (int)vector3.X, (int)(vector3.Y + vector4.Height));
            g.DrawLine(pen2, (int)(vector3.X + vector4.Width -1), (int)(vector3.Y + vector4.Height -1), (int)(vector3.X + vector4.Width-1), (int)(vector3.Y++));
            g.DrawLine(pen2, (int)(vector3.X + vector4.Width -1), (int)(vector3.Y + vector4.Height -1), (int)(vector3.X++), (int)(vector3.Y + vector4.Height-1));

            if (this.faceStyle == PerpetuumSoft.InstrumentationPDA.FaceStyle.RaisedNotch)
            {
                pen2 = new Pen(this.NotchLightColor);
                pen = new Pen(this.NotchDarkColor);
            }
            if (base.Checked)
            {
                if (base.Reverse)
                {
                    vector5 = new Vector(base.Direction, (vector.X + (vector2.Width / 2M)) - (this.LedSize.Width / 2), ((vector.Y + vector2.Y) - 3M) - this.LedSize.Height);
                }
                else
                {
                    vector5 = new Vector(base.Direction, (vector.X + (vector2.Width / 2M)) - (this.LedSize.Width / 2), vector.Y + 3M);
                }
                brush = new SolidBrush(this.activeLedColor);
            }
            else
            {
                if (base.Reverse)
                {
                    vector5 = new Vector(base.Direction, (vector.X + (vector2.Width / 2M)) - (this.LedSize.Width / 2), ((vector.Y + (vector2.Height / 2M)) - 3M) - this.LedSize.Height);
                }
                else
                {
                    vector5 = new Vector(base.Direction, (vector.X + (vector2.Width / 2M)) - (this.LedSize.Width / 2), (vector.Y + (vector2.Height / 2M)) + 3M);
                }
                brush = new SolidBrush(this.inactiveLedColor);
            }
            Vector vector6 = new Vector(base.Direction, this.LedSize.Width, this.LedSize.Height);
            g.FillRectangle(brush, base.Round(vector5.X), base.Round(vector5.Y), base.Round(vector6.X), base.Round(vector6.Y));
            if (this.FaceStyle != PerpetuumSoft.InstrumentationPDA.FaceStyle.Smooth)
            {
                decimal num;
                decimal num2;
                if (base.Reverse)
                {
                    num = ((vector2.Height / 2M) - 5M) - this.LedSize.Height;
                    num2 = vector.Y + 5M;
                }
                else
                {
                    num = (vector2.Height / 2M) - 2M;
                    num2 = (vector.Y + 8M) + this.LedSize.Height;
                }
                if (!flag)
                {
                    num += vector2.Height / 2M;
                    num2 += vector2.Height / 2M;
                }
                while (num2 <= num)
                {
                    Vector vector7 = new Vector(base.Direction, vector.X + 3M, num2);
                    vector6 = new Vector(base.Direction, (vector.X + vector2.Width) - 4M, num2);
                    g.DrawLine(pen2, (int) vector7.X, (int) vector7.Y, (int) vector6.X, (int) vector6.Y);
                    //vector7 = new Vector(base.Direction, vector.X + 3M, decimal.op_Increment(num2));
                    //vector6 = new Vector(base.Direction, (vector.X + vector2.Width) - 4M, decimal.op_Increment(num2));

                    vector7 = new Vector(base.Direction, vector.X + 3M, num2++);
                    vector6 = new Vector(base.Direction, (vector.X + vector2.Width) - 4M,num2++);
                    g.DrawLine(pen, (int) vector7.X, (int) vector7.Y, (int) vector6.X, (int) vector6.Y);
                    num2 += 3M;
                }
            }
        }

        protected override void DrawStatic(Graphics g)
        {
            base.DrawStatic(g);
            g.FillRectangle(new SolidBrush(this.BackColor), this.WorkRectangle);
        }

        public Color ActiveLedColor
        {
            get
            {
                return this.activeLedColor;
            }
            set
            {
                if (this.activeLedColor != value)
                {
                    this.activeLedColor = value;
                    base.Invalidate();
                }
            }
        }

        protected override Size DefaultControlSize
        {
            get
            {
                return new Size(40, 100);
            }
        }

        public Color FaceColor
        {
            get
            {
                return this.faceColor;
            }
            set
            {
                if (this.faceColor != value)
                {
                    this.faceColor = value;
                    base.Invalidate();
                }
            }
        }

        public PerpetuumSoft.InstrumentationPDA.FaceStyle FaceStyle
        {
            get
            {
                return this.faceStyle;
            }
            set
            {
                if (this.faceStyle != value)
                {
                    this.faceStyle = value;
                    base.Invalidate();
                }
            }
        }

        public Color InactiveLedColor
        {
            get
            {
                return this.inactiveLedColor;
            }
            set
            {
                if (this.inactiveLedColor != value)
                {
                    this.inactiveLedColor = value;
                    base.Invalidate();
                }
            }
        }

        public Size LedSize
        {
            get
            {
                return this.ledSize;
            }
            set
            {
                if (this.ledSize != value)
                {
                    this.ledSize = value;
                    base.Invalidate();
                }
            }
        }

        public Color NotchDarkColor
        {
            get
            {
                return this.notchDarkColor;
            }
            set
            {
                if (this.notchDarkColor != value)
                {
                    this.notchDarkColor = value;
                    base.Invalidate();
                }
            }
        }

        public Color NotchLightColor
        {
            get
            {
                return this.notchLightColor;
            }
            set
            {
                if (this.notchLightColor != value)
                {
                    this.notchLightColor = value;
                    base.Invalidate();
                }
            }
        }
    }
}

